-- nvim/lua/lua_function/poetry.lua
local M = {}

local poetry_library = {
  daytime = {
    "我是人间惆怅客，知君何事泪纵横。\n断肠声里忆平生。",
    "人生若只如初见，何事秋风悲画扇。",
    "海内存知己，天涯若比邻。",
    "他的猫和夹在书里的14行诗",
    "人是为明天活着的，因为记忆中有朝阳晓露",
    "在隆冬，我终于明白，\n我身上有个不可战胜的夏天",
    "我最终与命运和解,\n往后余生皆是暖阳",
  },
  nighttime = {
    "When the moonlight shines on the ground,\n The tree of life will be awaken",
    "书似青山常堆叠，灯如红豆最相思",
    "我本可以忍受黑暗，如果我不曾见过光明，\n可如今阳光把我的孤独照耀的更加荒凉。",
    "这是黄昏的太阳,\n我们却错把他当成了黎明的曙光",
    "生命的意义不在寻找答案，而在于体验问题本身\n",
    "我存在于万物之中,万物也存在于我心中,\n所有告别都是重逢",
    "你来了黑夜才听懂期待,\n你来了白昼才看破樊篱",
  },
}

function M.setup()
  vim.api.nvim_create_autocmd("User", {
    pattern = "ActuallyLoaded",
    once = true,
    callback = function()
      -- 启动诗词
      vim.defer_fn(function()
        local current_hour = tonumber(os.date("%H"))
        local time_type = (current_hour >= 7 and current_hour < 19) and "daytime" or "nighttime"
        local poems = poetry_library[time_type]
        local poem = poems[math.random(1, #poems)]
        vim.notify(poem, vim.log.levels.INFO, {
          timeout = 3000,
        })
      end, 500)

      vim.api.nvim_create_user_command("Poem", function()
        local current_hour = tonumber(os.date("%H"))
        local time_type = (current_hour >= 7 and current_hour < 19) and "daytime" or "nighttime"
        local poems = poetry_library[time_type]
        local poem = poems[math.random(1, #poems)]
        vim.notify(poem, vim.log.levels.INFO, {
          timeout = 5000,
        })
      end, { desc = "显示随机诗词" })

      vim.api.nvim_create_user_command("PoemDay", function()
        local poems = poetry_library.daytime
        local poem = poems[math.random(1, #poems)]
        vim.notify(poem, vim.log.levels.INFO, {
          timeout = 5000,
        })
      end, { desc = "显示白天诗词" })

      vim.api.nvim_create_user_command("PoemNight", function()
        local poems = poetry_library.nighttime
        local poem = poems[math.random(1, #poems)]
        vim.notify(poem, vim.log.levels.INFO, {
          timeout = 5000,
        })
      end, { desc = "显示夜间诗词" })
    end,
  })
end

return M
